package com.tmc.service.baseinfo;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.constant.CacheConstant;
import com.tmc.dao.impl.EthCarTypeDaoImpl;
import com.tmc.entity.baseinfo.CarType;
import com.tmc.service.cache.CacheServiceImpl;
import com.tmc.util.ObjectUtil;

/**
 * @author zhang
 * @date 2018年1月15日 下午3:35:27 
 */
@Service
@Transactional
public class EthCarTypeServiceImpl {
	
	@Autowired
	private EthCarTypeDaoImpl carTypeDao;
	
	@Autowired
	private CacheServiceImpl cache;
	
	private static String all_key = "KEY_CARTYPE_ALL";

	private Log logger = LogFactory.getLog(this.getClass());
	
	@SuppressWarnings("unchecked")
	public List<CarType> findAllFromCache(){
		if(cache.get(CacheConstant.CACHE_NAME_600, all_key) != null ){
			return (List<CarType>) cache.get(CacheConstant.CACHE_NAME_600, all_key);
		}
		List<CarType> list = carTypeDao.findAll();
		cache.put(CacheConstant.CACHE_NAME_600, all_key, list);
		return list;
	}
	
	@CacheEvict(value = CacheConstant.CACHE_NAME_600, key = "'KEY_CARTYPE_ALL'")
	public CommonResult delete(ContextInfo context, Long carTypeID){
		CarType carType = carTypeDao.getById(carTypeID);
		if(carType != null){
			ObjectUtil.updateBaseInfo(carType, context);
			carType.setEnabledFlag("N");
			carTypeDao.update(carType);
		}
		return CommonResult.makeSuccess("删除成功！");
	}
	
	public CarType getById(ContextInfo context, Long carTypeID) {
		return carTypeDao.getById(carTypeID);
	}
	
	@CacheEvict(value = CacheConstant.CACHE_NAME_600, key = "'KEY_CARTYPE_ALL'")
	public CommonResult save(ContextInfo context, CarType carType){
		CommonResult check = this.check(carType);
		if (!check.isSuccess()) {
			return check;
		}
		CarType dbCarType = null;
		if(carType.getCarTypeID() != null){
			dbCarType = carTypeDao.getById(carType.getCarTypeID());
		}else if(carType.getYitu8CarTypeID() !=null){
			dbCarType = carTypeDao.getByYitu8CarTypeID(carType.getYitu8CarTypeID());
		}
		
		if(dbCarType == null){
			ObjectUtil.setBaseInfo(carType, context);
			carTypeDao.insert(carType);
		}else{
			// 编辑
			try {
				if (dbCarType != null) {
					ObjectUtil.copyProperties(carType, dbCarType,
							new String[] { "createdBy", "creationDate", "carTypeID", "yitu8CarTypeID", "enabledFlag" });
				}
				ObjectUtil.updateBaseInfo(dbCarType, context);
				carTypeDao.update(dbCarType);
			} catch (Exception e) {
				logger.error("拷贝对象错误！", e);
				return CommonResult.makeFail("拷贝对象错误！");
			}
		}
		return CommonResult.makeSuccess();
	}

	private CommonResult check(CarType carType) {
		if(ObjectUtil.noText(carType.getName())){
			return CommonResult.makeFail("名字不能为空！");
		}
		if(carType.getMaxLuggage() != null){
			return CommonResult.makeFail("最大行旅不能为空！");
		}
		if(carType.getMaxPassenger() != null){
			return CommonResult.makeFail("最大乘客不能为空！");
		}
		return CommonResult.makeSuccess();
	}
}
